<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


/**
 * Class Offer
 * @package app\admin\controller 股票代码和报价维护
 */
class Offer extends Base
{
    public  $model;

    public function __construct()
    {
        $this->model = model("Shares");
        parent::__construct();
    }


        public function getBaseExtModel(){
                    $BaseExtModel = $this->model->BaseExtModel;
                    if(empty($BaseExtModel)){
                        return failJson("暂无数据");
                    }
                    foreach($BaseExtModel as $k=>&$v){
                        if(isset($v['Model'])){
                            unset($v['Model']);
                        }
                        if(isset($v['on'])){
                            unset($v['on']);
                        }
                    }
            return   sucessJson("查询成功",$BaseExtModel);

        }


    /**
     * 显示股票代码
     *
     * @return \think\Response
     */
    public function index()
    {//
        /**
         *  搜索 @股票代码
         *  搜索 @股票名称
         */
            $where = [];
        if(isset($this->code)){
            $where['a.code'] = $this->code;
        }
            w_s("no_in",true);
            $res = $this->model->defaultPage($where);
            if (empty($res)){
                return failJson("暂无股票数据");
            }
        return   resJson(1,"查询成功",$res['res'],$res['pageInfo']);


    }

    /**
     *  添加CODE
     */
    public function  save(){
                $data = $this->RealData;//处理MARK_Price的数据
                $k_data = [];  //status数据
                $k_code_data = []; //交集数据
        /**
         *  处理状态的改变
         */
        if(isset($data['status'])){
            $k_data['status'] = $data['status'];
            unset($data['status']);
        }

        $atrrCode = $this->attributeCode;
        $arr = ['SH',"SZ"];
        if(!in_array($atrrCode,$arr)){
            return failJson("股票属性不能为空");
        }
        unset($data['attributeCode']);
        $data['attr'] = $atrrCode;

        /**
         *  处理code
         */
        $k_code_data['code'] = $data['code'];
        unset($data['code']);
        $count = $this->model->baseCount($k_code_data);
        /**
         *  code的排除机制
         */
        if($count){
                return failJson("该股票代码已经存在，请添加不存在的股票代码");
        }
        /*
         7 sh
        8 sz*/
        if(isset($data['name'])){
            $k_code_data['name'] = $data['name'];
            unset($data['name']);
        }
        $res = $this->model->saveOffer($data, $k_code_data, $k_data);
        if(!$res['status']){
            return failJson($res['msg']);
        }
        return sucessJson($res['msg']);
    }


    /**
     *  修改股票代码
     */
    public function upcode(){
        $data = $this->RealData;//处理MARK_Price的数据

        $k_data = [];  //status数据
        $k_code_data = []; //交集数据

        /**
         *  处理状态的改变
         */
        if(isset($data['status'])){
            $k_data['status'] = $data['status'];
            unset($data['status']);
        }

        if(isset($this->attributeCode)){
            $atrrCode = $this->attributeCode;
            unset($data['attributeCode']);
            $arr = ['SH',"SZ"];
            if(!in_array($atrrCode,$arr)){
                return failJson("股票属性不能为空");
            }
            $data['attr'] = $atrrCode;
        }

        /**
         *  处理code
         */
        $k_code_data['code'] = $data['code'];
        unset($data['code']);
        $count = $this->model->baseCount($k_code_data);
        /**
         *  code的排除机制
         */
        if(!$count){
            return failJson("该股票代码不存在，请新增股票配置");
        }

        if(isset($data['name'])){
            $k_code_data['name'] = $data['name'];
            unset($data['name']);
        }


       $res = $this->model->saveUpOffer($data, $k_code_data, $k_data);

        if(!$res['status']){
                return failJson($res['msg']);
        }
        return sucessJson($res['msg']);
    }



}
